package com.example.skillcinema.adapters

import com.example.skillcinema.R
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.skillcinema.databinding.YearPickerItemBinding
import com.example.skillcinema.viewholders.YearPickerItemViewHolder

class YearPickerRecyclerViewAdapter(val onClick: (year: Int) -> Unit) :
    RecyclerView.Adapter<YearPickerItemViewHolder>() {
    private var values: List<Int> = listOf()
    private var clicked: Int = 0

    fun setData(list: List<Int>) {
        values = list
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YearPickerItemViewHolder =
        YearPickerItemViewHolder(
            YearPickerItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )

    override fun getItemCount(): Int = values.count()

    override fun onBindViewHolder(holder: YearPickerItemViewHolder, position: Int) {
        val item: Int = values[position]
        val index = position
        with(holder.binding) {
            if (clicked == index) {
                yearButton.setTextAppearance(com.google.android.material.R.style.Widget_Material3_Button)
                yearButton.setBackgroundResource(R.drawable.year_picked)
            } else {
                yearButton.setTextAppearance(android.R.style.TextAppearance)
                yearButton.setBackgroundResource(0)
            }
            yearButton.text = item.toString()
            yearButton.setOnClickListener {
                clicked = index
                onClick(item)
                notifyDataSetChanged()
            }
        }
    }
}